package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallProductCategoryEntity;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.lahuobao.mall.service.admin.MallProductCategoryServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import java.util.List;

@Service("mallProductCategoryService")
@Transactional
public class MallProductCategoryServiceImpl extends CommonServiceImpl implements MallProductCategoryServiceI {

    @Override
    public String getMallProductCategoryTree() {
        List<MallProductCategoryEntity> list = getList(MallProductCategoryEntity.class);
        JSONArray json = new JSONArray();
        for (MallProductCategoryEntity productCategory : list) {
            JSONObject ob = new JSONObject();
            ob.put("id", productCategory.getId());
            ob.put("pId", productCategory.getParentcategoryid());
            ob.put("name", productCategory.getCategoryname());
            ob.put("isTop", productCategory.getIstop());
            ob.put("doCheck", false);
            ob.put("nocheck", productCategory.getParentcategoryid() == 0 ? false : true);
            ob.put("checked", productCategory.getIstop() == 1 ? true : false );
            ob.put("categorycode", productCategory.getCategorycode() );
            json.add(ob);
        }
        return json.toString();
    }

    /**
     * 根据分类code查询
     * @return
     */
    @Override
    public MallProductCategoryEntity selectByCode(String cateCode) {
        return  super.findUniqueByProperty(MallProductCategoryEntity.class, "categorycode", cateCode);
    }
}